Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

224
Vistas
Entityframework > Get tracked entity by key (different reference)

I'm in need of the functionality to retrieve a tracked entity by key without a SQL call to the DB. So I need something like the .Find but if it's not tracked NULL is returned.

The reason why I need this is because following code throws:

public void Test()
{
    var id = 5;

    var a = new Entity { Id = id };
    _ctx.Entry(a).State = EntityState.Added;

    var b = new Entity { Id = id };
    var bEntry = _ctx.Entry(b);

    // Delete entity
    // When it was already added in this 'lifetime' detach it again (since it's not saved in the DB yet)
    if (bEntry.State == EntityState.Added)
        bEntry.State = EntityState.Detached;
    else
        bEntry.State = EntityState.Deleted;
    
    var c = new Entity { Id = id };
    var cEntry = _ctx.Entry(c);
    
    // Add entity
    // When it was already deleted in this 'lifetime' detach it again (since it's not removed in the DB yet)
    if (cEntry.State == EntityState.Deleted)
        cEntry.State = EntityState.Detached;
    else
        cEntry.State = EntityState.Added;
    
    var d = new Entity { Id = id };
    var dEntry = _ctx.Entry(d);
}

If you would never create new entities but always use a. This code will work. The reason why it doesn't work now, is because internally EF tracks the entities in a dictionary using a 'by ref compare' (https://github.com/dotnet/efcore/blob/9ac01d6035c76626d89aa1a3cd8d200db2c3c0e1/src/EFCore/ChangeTracking/Internal/EntityReferenceMap.cs#L103)

As a result the TryGetValue EF uses will be false and it will create a new entry with state Detached. And later the code will crash because I try to add a entity that is already tracked.

This above problem would be solved if I could get the tracked entity since they would have the same ref. But I don't seem to find a way to access the same code that happens in .Find(). I could in theory use the .Local property or get it by using ChangeTracker.Entries() but since this are just regular lists the performance to lookup the entry is taking way to long.

I know this is a very specific question. But I don't really seem to find anything. It's a bit a shame to give up since internally EF knows what I need.

Brecht

over 4 years ago · Santiago Trujillo
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda